#include "memory_demo_integrity.h"

#define TEST_POOL_SIZE (8*1024)
static __attribute__((aligned(4))) UINT8 g_testDynPool[TEST_POOL_SIZE];

void task_mem_integrity()
{
    printf("-----task memory integrity-----\n");
    /* 申请两个物理连续的内存块 */
    uint8_t *ptr1 = (uint8_t *)LOS_MemAlloc(g_testDynPool, 8);
    if(ptr1 == NULL){printf("malloc1 failed\n");}
    uint8_t *ptr2 = (uint8_t *)LOS_MemAlloc(g_testDynPool, 8);
    if(ptr2 == NULL){printf("malloc2 failed\n");}
    /* 第一个节点内存块大小是8字节，那么12字节的清零，会踩到第二个内存节点的节点头，构造踩内存场景 */
    memset(ptr1, 0, 16);
    LOS_MemIntegrityCheck(g_testDynPool);
}
UINT32 memory_entry_integrity(void)
{
    uint32_t ret, tid;
        // 初始化内存池
    ret = LOS_MemInit(g_testDynPool, TEST_POOL_SIZE);
    if (LOS_OK  == ret) { printf("Mem init success!\n"); }
    else { printf("Mem init failed!\n"); return; }
    TSK_INIT_PARAM_S param = {0};
    param.pfnTaskEntry = (TSK_ENTRY_FUNC)task_mem_integrity;
    param.uwStackSize  = 0x1000;
    param.pcName       = "task_mem_integrity";
    param.usTaskPrio   = 10;
    // 锁住任务调度，防止高优先级任务调度
    LOS_TaskLock();
    // 创建任务
    ret = LOS_TaskCreate(&tid, &param);
    if (ret != LOS_OK) {
        printf("task memory integrity create failed, errno = 0x%x\n", ret);
    }
    // 解锁任务调度
    LOS_TaskUnlock();
    return ret;
}
